using BasicScript.Component;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class BabyClothingItem : BaseItem
	{
		public enum State
		{
			Fold = 0,
			Unfold = 1
		}

		public State state;

		public Transform body;

		public int topsIconId;

		private bool isInitBody;

		private SpriteAssets spriteAssets;

		private Transform icon;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			body = model.Find("Body");
			icon = model.Find("Icon");
			spriteAssets = base.transform.GetComponent<SpriteAssets>();
			SetFoldSprite(base.itemData.topsId);
		}

		public void SetState(State val)
		{
			if (state != val)
			{
				state = val;
				if (state == State.Fold)
				{
					icon.gameObject.SetActive(true);
					body.gameObject.SetActive(false);
					SetPlatformColliderEnable(true);
				}
				else
				{
					icon.gameObject.SetActive(false);
					InitBody();
					body.gameObject.SetActive(true);
					SetPlatformColliderEnable(false);
				}
			}
		}

		public void InitBody()
		{
			if (!isInitBody)
			{
				isInitBody = true;
				Transform transform = Singleton<AssetBundleManager>.Instance.characterResource.transform;
				ChangeSprite(body.Find("Sprite"), transform.Find("Model/Body/BabyClothing/" + base.itemData.topsId));
			}
		}

		public override bool CanAttatchItem()
		{
			if (childItems.Count > 0)
			{
				return false;
			}
			return base.CanAttatchItem();
		}

		public void SetFoldSprite(int id)
		{
			SpriteRenderer component = icon.GetComponent<SpriteRenderer>();
			component.sprite = spriteAssets.GetSprite(id.ToString());
			icon.localPosition = new Vector3(0f, component.bounds.size.y / 2f, 0f);
		}

		private void ChangeSprite(Transform targetTrans, Transform spriteTrans)
		{
			if (spriteTrans == null)
			{
				targetTrans.GetComponent<SpriteRenderer>().sprite = null;
				return;
			}
			targetTrans.GetComponent<SpriteRenderer>().sprite = spriteTrans.GetComponent<SpriteRenderer>().sprite;
			targetTrans.localPosition = spriteTrans.localPosition;
		}

		public override void ResetState()
		{
			base.ResetState();
			CheckState();
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			if (childItems.Count < 1)
			{
				SetState(State.Unfold);
			}
			base.TouchDown(touchEventData);
		}

		public void CheckState()
		{
			base.gameObject.SetActive(true);
			if (parentItem != null && (parentItem.itemData.type == 3 || parentItem.itemData.scriptType == 17))
			{
				SetState(State.Unfold);
			}
			else
			{
				SetState(State.Fold);
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			CheckState();
		}

		protected override void OnTriggerEnter2D(Collider2D collision)
		{
			if (isDrag && state == State.Unfold && collision.gameObject.layer == 12)
			{
				ItemComponentTrigger component = collision.gameObject.GetComponent<ItemComponentTrigger>();
				if (component != null && (component.type == ItemComponentTrigger.Type.LeftHand || component.type == ItemComponentTrigger.Type.RightHand))
				{
					return;
				}
			}
			base.OnTriggerEnter2D(collision);
		}

		protected override void CheckColls()
		{
			if (childItems.Count < 1)
			{
				for (int i = 0; i < itemCompColls.Count; i++)
				{
					ItemComponentTrigger.Type type = itemCompColls[i].trigger.type;
					if (type == ItemComponentTrigger.Type.Body && itemCompColls[i].trigger.owner.itemData.scriptType == 123 && ((BabyItem)itemCompColls[i].trigger.owner).ChangeTops(this))
					{
						GlobalEventSystem.Fire(new BaseEvent("game.remove_item", archiveData.index));
						return;
					}
				}
			}
			base.CheckColls();
		}

		public override bool CanPlaceItem(BaseItem item)
		{
			if (state == State.Unfold || childItems.Count > 0)
			{
				return false;
			}
			return base.CanPlaceItem(item);
		}

		protected override bool CanPlaceOnPlatform(BaseItem item)
		{
			if (childItems.Count > 0 && item.itemData.type == 3)
			{
				return false;
			}
			return base.CanPlaceOnPlatform(item);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			obj["17"] = topsIconId;
		}
	}
}
